﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{ // NOTE: If you change the class name "Education" here, you must also update the reference to "Education" in App.config.
    public partial class MainService : IEducation
    {
        public DataSet Education_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Education_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Education_GetByEmployeeID(int _EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Education_GetByEmployeeID", dbConn);
            dbCmd.Parameters.AddWithValue("@Education_EmployeeID", _EmployeeID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Education_GetList(out int ItemCount, int currentPage, int pageSize, int branchID, int departmentID, int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Education_GetList", dbConn);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@BranchID", branchID);
            dbCmd.Parameters.AddWithValue("@DepartmentID", departmentID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public EducationInfo Education_GetInfo(int EducationID)
        {
            EducationInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Education_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Education_ID", EducationID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new EducationInfo();
                    retVal.ID = Convert.ToInt32(dr["Education_ID"]);
                    retVal.BranchID = Convert.ToInt32(dr["Education_BranchID"]);
                    retVal.DepartmentID = Convert.ToInt32(dr["Education_DepartmentID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["Education_EmployeeID"]);
                    retVal.Name = dr["Education_Name"].ToString();
                    retVal.Kind = dr["Education_Kind"].ToString();
                    retVal.Major = dr["Education_Major"].ToString();
                    retVal.Foreign = Convert.ToBoolean(dr["Education_Foreign"]);
                    retVal.Place = dr["Education_Place"].ToString();
                    retVal.FromDate = dr["Education_FromDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["Education_FromDate"]);
                    retVal.Time = dr["Education_Time"].ToString();
                    //retVal.FinishDate = dr["Education_FinishDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["Education_FinishDate"]);
                    retVal.DateIssue = dr["Education_DateIssue"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["Education_DateIssue"]);
                    retVal.AuthorIssue = dr["Education_AuthorIssue"].ToString();
                    retVal.Rank = dr["Education_Rank"].ToString();
                    retVal.Description = dr["Education_Description"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Education_Insert(EducationInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Education_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Education_BranchID", eInfo.BranchID);
            dbCmd.Parameters.AddWithValue("@Education_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@Education_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Education_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@Education_Kind", eInfo.Kind);
            dbCmd.Parameters.AddWithValue("@Education_Major", eInfo.Major);
            dbCmd.Parameters.AddWithValue("@Education_Foreign", eInfo.Foreign);
            dbCmd.Parameters.AddWithValue("@Education_Place", eInfo.Place);
            dbCmd.Parameters.AddWithValue("@Education_Time", eInfo.Time);
            if (eInfo.FromDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Education_FromDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Education_FromDate", eInfo.FromDate);
            if (eInfo.DateIssue == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Education_DateIssue", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Education_DateIssue", eInfo.DateIssue);
            dbCmd.Parameters.AddWithValue("@Education_AuthorIssue", eInfo.AuthorIssue);
            dbCmd.Parameters.AddWithValue("@Education_Rank", eInfo.Rank);
            dbCmd.Parameters.AddWithValue("@Education_Description", eInfo.Description);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Education_Update(EducationInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Education_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Education_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Education_BranchID", eInfo.BranchID);
            dbCmd.Parameters.AddWithValue("@Education_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@EDUCATION_EMPLOYEEID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Education_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@Education_Kind", eInfo.Kind);
            dbCmd.Parameters.AddWithValue("@Education_Major", eInfo.Major);
            dbCmd.Parameters.AddWithValue("@Education_Foreign", eInfo.Foreign);
            dbCmd.Parameters.AddWithValue("@Education_Place", eInfo.Place);
            dbCmd.Parameters.AddWithValue("@Education_Time", eInfo.Time);
            if (eInfo.FromDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Education_FromDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Education_FromDate", eInfo.FromDate);
            if (eInfo.DateIssue == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Education_DateIssue", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Education_DateIssue", eInfo.DateIssue);
            dbCmd.Parameters.AddWithValue("@Education_AuthorIssue", eInfo.AuthorIssue);
            dbCmd.Parameters.AddWithValue("@Education_Rank", eInfo.Rank);
            dbCmd.Parameters.AddWithValue("@Education_Description", eInfo.Description);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Education_Delete(int EducationID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Education_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Education_ID", EducationID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
